import { mockRequestClient as requestClient } from '#/api/request';
import type {
  ResourceLifecycleStatus,
  ResourceAvailabilityStatus,
  ResourceVersionStatus,
  BehaviorExecutionStatus,
  PublishStatus,
  EventType,
  ResourceStatus,
  PublishStatusInfo,
  BehaviorStatus,
  Event,
  StatusChangeHistory,
} from './status-types';

enum Api {
  // 资源状态管理
  ResourceLifecycleStatus = '/resource-center/resources/{resourceId}/status/lifecycle',
  ResourceAvailabilityStatus = '/resource-center/resources/{resourceId}/status/availability',
  ResourceVersionStatus = '/resource-center/resources/{resourceId}/status/version',
  ResourceStatusHistory = '/resource-center/resources/{resourceId}/status/history',

  // 行为状态管理
  BehaviorStatus = '/resource-center/behaviors/{behaviorId}/status',
  BehaviorExecutionHistory = '/resource-center/behaviors/{behaviorId}/execution-history',

  // 发布状态管理
  PublishStatus = '/resource-center/resources/{resourceId}/publish-status',
  CanaryDeploy = '/resource-center/resources/{resourceId}/canary-deploy',

  // 事件管理
  Events = '/resource-center/events',
  ResourceEvents = '/resource-center/resources/{resourceId}/events',
  Compensation = '/resource-center/resources/{resourceId}/behaviors/{behaviorId}/compensate',
}

// ==================== 资源状态管理 ====================

// 更新资源生命周期状态
export function updateResourceLifecycleStatus(
  resourceId: string,
  status: ResourceLifecycleStatus,
  reason?: string,
) {
  return requestClient.put(
    Api.ResourceLifecycleStatus.replace('{resourceId}', resourceId),
    { status, reason },
  );
}

// 更新资源可用性状态
export function updateResourceAvailabilityStatus(
  resourceId: string,
  status: ResourceAvailabilityStatus,
  reason?: string,
) {
  return requestClient.put(
    Api.ResourceAvailabilityStatus.replace('{resourceId}', resourceId),
    { status, reason },
  );
}

// 更新资源版本状态
export function updateResourceVersionStatus(
  resourceId: string,
  status: ResourceVersionStatus,
  reason?: string,
) {
  return requestClient.put(
    Api.ResourceVersionStatus.replace('{resourceId}', resourceId),
    { status, reason },
  );
}

// 获取资源状态历史
export function getResourceStatusHistory(resourceId: string) {
  return requestClient.get<StatusChangeHistory[]>(
    Api.ResourceStatusHistory.replace('{resourceId}', resourceId),
  );
}

// 获取资源完整状态
export function getResourceStatus(resourceId: string) {
  return requestClient.get<ResourceStatus>(
    `/resource-center/resources/${resourceId}/status`,
  );
}

// ==================== 行为状态管理 ====================

// 更新行为状态
export function updateBehaviorStatus(
  behaviorId: string,
  status: BehaviorExecutionStatus,
  errorMessage?: string,
) {
  return requestClient.put(
    Api.BehaviorStatus.replace('{behaviorId}', behaviorId),
    { status, errorMessage },
  );
}

// 获取行为执行历史
export function getBehaviorExecutionHistory(behaviorId: string) {
  return requestClient.get<BehaviorStatus[]>(
    Api.BehaviorExecutionHistory.replace('{behaviorId}', behaviorId),
  );
}

// ==================== 发布状态管理 ====================

// 更新发布状态
export function updatePublishStatus(
  resourceId: string,
  status: PublishStatus,
  version?: string,
) {
  return requestClient.put(
    Api.PublishStatus.replace('{resourceId}', resourceId),
    { status, version },
  );
}

// 金丝雀发布
export function deployCanary(resourceId: string, percentage: number) {
  return requestClient.post(
    Api.CanaryDeploy.replace('{resourceId}', resourceId),
    { percentage },
  );
}

// 获取发布状态
export function getPublishStatus(resourceId: string) {
  return requestClient.get<PublishStatusInfo>(
    `/resource-center/resources/${resourceId}/publish-status`,
  );
}

// ==================== 事件管理 ====================

// 获取事件列表
export function getEvents(params?: {
  type?: EventType;
  sourceId?: string;
  sourceType?: string;
  startTime?: string;
  endTime?: string;
  page?: number;
  pageSize?: number;
}) {
  return requestClient.get<{ items: Event[]; total: number }>(
    Api.Events,
    { params },
  );
}

// 获取资源事件
export function getResourceEvents(
  resourceId: string,
  eventTypes?: EventType[],
) {
  return requestClient.get<Event[]>(
    Api.ResourceEvents.replace('{resourceId}', resourceId),
    { params: { types: eventTypes } },
  );
}

// 触发补偿
export function triggerCompensation(resourceId: string, behaviorId: string) {
  return requestClient.post(
    Api.Compensation
      .replace('{resourceId}', resourceId)
      .replace('{behaviorId}', behaviorId),
  );
}

// 重放事件
export function replayEvent(eventId: string) {
  return requestClient.post(`/resource-center/events/${eventId}/replay`);
}
